package core.socket
{
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.system.Security;
	
	public class SocketServiceBase extends EventDispatcher implements ISocketService
	{
		public function SocketServiceBase()
		{
			super();
		}
		
		public function dispose():void
		{
			removeEvents();
		}
		
		override protected function get packetClassRef():Class
		{
			return SocketDataPacket;
		}
		
		protected var socketValue:Socket;
		
		protected var _ip:String;
		
		protected var _port:int = 0;
		
		public function connect(ip:String, port:int):void
		{
			_ip = ip;
			_port = port;
			
			var xmlStr:String = "xmlsocket://";
			xmlStr += _ip;
			xmlStr += ":";
			xmlStr += (_port + 1);
			socketValue = new Socket();
			Security.loadPolicyFile(xmlStr);
			addEvents();
			socketValue.connect(_ip, _port);
		}
		
		public function closeConnect():void
		{
			socketValue.close();
		}
		
		public function get isConnect():Boolean
		{
			return false;
		}
		
		protected function removeEvents():void
		{
			socketValue.removeEventListener(Event.CONNECT, onConnect);
			socketValue.removeEventListener(Event.CLOSE, onClose);
			socketValue.removeEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
			socketValue.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			socketValue.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		}
		
		protected function addEvents():void
		{
			socketValue.addEventListener(Event.CONNECT, onConnect);
			socketValue.addEventListener(Event.CLOSE, onClose);
			socketValue.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
			socketValue.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			socketValue.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		}
		
		protected function onSecurityError(evt:SecurityErrorEvent)
		{
			
		}
		
		protected function onIOError(evt:Event):void 
		{
			
		}
		
		protected function onSocketData(evt:ProgressEvent):void
		{
			var message:String = socketValue.readMultiByte(socketValue.bytesAvailable, "utf-8");
			getMessage(message);
		}
		
		protected function getMessage(value:String):void
		{
			var packet:SocketDataPacket = new packetClassRef();
			packet.parse(value);
		}
		
		protected function onConnect(evt:Event):void
		{
			
		}
		
		protected function onClose(evt:Event):void
		{
			
		}
		
		public function send(key:String , params:* = null):void
		{
			var packet:SocketDataPacket = new packetClassRef();
			packet.create();
			
			try
			{
				socketValue.writeMultiByte("|head^" + packet.source + "^end|", "utf-8");
				socketValue.flush();
			}
			catch (error:Error)
			{
				trace(error.toString());
			}
		}
	}
}